﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;


namespace 李晓霞web
{
    public partial class StudentInformationManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String strCn = "Server=pc.hstc.cn;database=dhd2014112106;uid=2014112106;pwd=19950616";
            SqlConnection cn = new SqlConnection(strCn); //建对象
            try
            {
                cn.Open();//打开连接
               
                string strSql = "SELECT   Name,Gender, Class,CollegeId,Memo FROM   Student  WHERE   (No = N'" + this.txtNo.Text + "')";
               
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                   
                    this.txtName.Text = dr["Name"].ToString();

                    this.DropDownList1.Text = dr["Gender"].ToString();
                    this.txtClass.Text = dr["Class"].ToString();
                    this.txtCollegeId.Text = dr["CollegeId"].ToString();
                    this.txtMemo.Text = dr["Memo"].ToString();
                }
               
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void btnNameSelect_Click1(object sender, EventArgs e)
        {
            String strCn = "Server=pc.hstc.cn;database=dhd2014112106;uid=2014112106;pwd=19950616";
            SqlConnection cn = new SqlConnection(strCn); 
            try
            {
                cn.Open();
                string strName = this.txtName.Text;
               
                string strSql = "SELECT   No,Gender, Class,CollegeId,Memo FROM      Student  WHERE   (Name = N'" + this.txtName.Text + "')";
               
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                 
                    this.txtNo.Text = dr["No"].ToString();
                    this.DropDownList1.Text = dr["Gender"].ToString();
                    this.txtClass.Text = dr["Class"].ToString();
                    this.txtCollegeId.Text = dr["CollegeId"].ToString();
                    this.txtMemo.Text = dr["Memo"].ToString();
                }
                
                
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            String strCn = "Server=pc.hstc.cn;database=dhd2014112106;uid=2014112106;pwd=19950616";
            SqlConnection cn = new SqlConnection(strCn); 
            try
            {
                cn.Open();
                string strSql = "UPDATE Student SET Name=N'" + this.txtName.Text + "',Gender=N'" + this.DropDownList1.Text + "',Class=N'" + this.txtClass.Text + "',CollegeId=N'" + this.txtCollegeId.Text + "',Memo=N'" + this.txtMemo.Text + "'WHERE (No=N'" + this.txtNo.Text + "')";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                int iUpd = cmd.ExecuteNonQuery();
                if (iUpd < 1)
                {
                    this.lblInfo.Text = "修改失败！";
                }
                else
                {
                    this.lblInfo.Text = "修改了" + iUpd.ToString() + "条记录！";
                }
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            String strCn = "Server=pc.hstc.cn;database=dhd2014112106;uid=2014112106;pwd=19950616";
            SqlConnection cn = new SqlConnection(strCn); 
            try
            {
                cn.Open();
                string strSql = "INSERT INTO Student(No, Name, Gender, Class, CollegeId, Memo) VALUES   (N'" + this.txtNo.Text + "',N'" + this.txtName.Text + "',N'" + this.DropDownList1.Text + "',N'" + this.txtClass.Text + "',N'" + this.txtCollegeId.Text + "',N'" + this.txtMemo.Text + "')";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                int iUpd = cmd.ExecuteNonQuery();
                if (iUpd < 1)
                {
                    this.lblInfo.Text = "不好意思，新增失败!";
                }
                else
                {
                    this.lblInfo.Text = "感谢您新增成功!";
                }
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void btnRead_Click(object sender, EventArgs e)
        {
            String strCn = "Server=pc.hstc.cn;database=dhd2014112106;uid=2014112106;pwd=19950616";
            SqlConnection cn = new SqlConnection(strCn); //
            try
            {
                cn.Open();
                string strSql = "DELETE FROM Student WHERE  (No= N'" + this.txtNo.Text + "')";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                int iDelete = cmd.ExecuteNonQuery();
                if (iDelete < 1)
                {
                    this.lblInfo.Text = "不好意思，删除失败！";
                }
                else
                {
                    this.lblInfo.Text = "恭喜您！删除了" + iDelete.ToString() + "条记录！";
                }
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void btnAgain_Click(object sender, EventArgs e)
        {
            this.txtClass.Text = "";
            this.txtCollegeId.Text = "";
            this.txtMemo.Text = "";
            this.txtName.Text = "";
            this.txtNo.Text = "";
            this.DropDownList1.Text = "";
        }

        protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            String strCn = "Server=pc.hstc.cn;database=dhd2014112106;uid=2014112106;pwd=19950616";
            SqlConnection cn = new SqlConnection(strCn);
            try 
            {
                cn.Open();
            
                string strSql = " SELECT   *   FROM     Student";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlDataReader dr = cmd.ExecuteReader();
             
                this.GridView1.DataSource = dr;
                this.GridView1.DataBind();
                dr.Close();
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }
    }
}